package org.millioncall.yueyoga.admin.service.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.yueyoga.admin.dao.ProductRemainingDAO;
import org.millioncall.yueyoga.admin.model.MemberLevel;
import org.millioncall.yueyoga.admin.model.ProductRemaining;
import org.millioncall.yueyoga.admin.model.ProductUsedInfo;
import org.millioncall.yueyoga.admin.service.ProductRemainingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class ProductRemainingServiceImpl implements ProductRemainingService {
	private static final Logger logger = LoggerFactory
			.getLogger(MemberLevel.class);
	private ProductRemainingDAO productRemainingDAO;

	@Override
	public boolean saveProductRemaining(ProductRemaining productRemaining) {
		return productRemainingDAO.save(productRemaining) > 0;
	}

	@Override
	public boolean updateProductRemaining(Map<String, Object> map) {
		return productRemainingDAO.update(map) > 0;
	}

	@Override
	public void deleteProductRemainingByIds(Integer[] deids) {
		if (deids == null || deids.length == 0) {
			logger.error("未提交要删除演讲者的编号");
			throw new RuntimeException(" 未提交要删除演讲者的编号");
		} else {
			this.productRemainingDAO.deleteByIds(deids);
		}
	}

	@Override
	public void deleteProductRemainingByMemberIds(Integer[] deids) {
		if (deids == null || deids.length == 0) {
			logger.error("未提交要删除演讲者的编号");
			throw new RuntimeException(" 未提交要删除演讲者的编号");
		} else {
			this.productRemainingDAO.deleteByMemberIds(deids);
		}
	}

	@Override
	public PaginatedList<ProductRemaining> getProductRemainingInPage(int start,
			int pageSize, Map<String, Object> map) {
		PaginatedList<ProductRemaining> p = productRemainingDAO.findPage(start,
				pageSize, map);
		return p;
	}

	@Override
	public int getCountsByMembetId(Map<String, Object> map) {
		return productRemainingDAO.getCountsByMembetId(map);
	}

	@Override
	public PaginatedList<ProductRemaining> findMemberProduct(int start,
			int limit, Map<String, Object> map) {
		return productRemainingDAO.findMemberProduct(start, limit, map);
	}

	@Override
	public PaginatedList<ProductRemaining> findProductByMemberId(Integer start,
			Integer limit, Integer memberId) {
		return this.productRemainingDAO.findProductByMemberId(start, limit,
				memberId);
	}

	/**
	 * 查询用户产品的使用情况
	 */
	@Override
	public PaginatedList<ProductUsedInfo> findMemberProductsUsed(int start,
			int limit, Map<String, Object> map) {
		return productRemainingDAO.findMemberProductsUsed(start, limit, map);
	}

	/**
	 * 退款
	 */
	@Override
	public boolean refundProductsByIds(String proRemainIds) {
		if (StringUtils.isNotBlank(proRemainIds)) {
			String[] proRIds = proRemainIds.split(",");
			Integer[] prIds = new Integer[proRIds.length];
			for (int i = 0; i < proRIds.length; i++) {
				prIds[i] = Integer.parseInt(proRIds[i]);
			}

			// --循环删除产品使用情况记录
			if (proRIds != null && proRIds.length > 0) {
				try {
					productRemainingDAO.deleteByIds(prIds);
					return true;
				} catch (Exception e) {
					return false;
				}
			}
		}
		return false;
	}

	public ProductRemainingDAO getProductRemainingDAO() {
		return productRemainingDAO;
	}

	public void setProductRemainingDAO(ProductRemainingDAO productRemainingDAO) {
		this.productRemainingDAO = productRemainingDAO;
	}

	@Override
	public ProductRemaining findProductRemainingById(int promainId) {
		return productRemainingDAO.findProductRemainingById(promainId);
	}

	@Override
	public boolean saveClearOrder(ProductRemaining pr) {
		return this.productRemainingDAO.saveClearOrder(pr);
	}

	@Override
	public PaginatedList<ProductRemaining> findConsumptionProductRemaining(
			Map<String, Object> map, Integer start, Integer limit) {
		return this.productRemainingDAO.findConsumptionProductRemaining(map,
				start, limit);
	}

	@Override
	public boolean deleteByIds(String Ids) {
		return this.productRemainingDAO.deleteByIds(Ids);
	}

	@Override
	public boolean updateProductRemaining(ProductRemaining pr) {
		try {
			this.productRemainingDAO.updateProductRemaining(pr);
			return true;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
